package cn.dong.edu.controller.api;

import cn.dong.edu.domain.Teacher;
import cn.dong.edu.service.impl.TeacherServiceImpl;
import cn.dong.api.domain.CommonResult;
import cn.dong.edu.domain.Course;
import cn.dong.edu.service.impl.CourseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/api/edu/teacher")
@Slf4j
@RestController
public class ApiTeacherController {

    @Autowired
    private TeacherServiceImpl teacherService;
    @Autowired
    private CourseServiceImpl courseService;

    /**
     * 查询全部讲师
     * @return
     */
    @GetMapping("/findAll")
    public CommonResult<List<Teacher>> findAll(){
        return CommonResult.ok().data(teacherService.list());
    }


    /**
     * 根据分页查询全部讲师！
     * @return
     */
    @GetMapping("/findAllByPage/{current}/{size}")
    public CommonResult<List<Teacher>> findAllByPage(@PathVariable Integer current,
                                                     @PathVariable Integer size){
        // 配置分页
        IPage<Teacher> page = new Page<>(current, size);
        return CommonResult.ok().data(teacherService.page(page));
    }

    /**
     * 根据id查询讲师
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public CommonResult<Teacher> findById(@PathVariable String id){
        return CommonResult.ok().data(teacherService.getById(id));
    }

    /**
     * 根据讲师id查询他讲的课，并且已发布
     * @param id
     * @return
     */
    @GetMapping("/findCourseById/{id}")
    public CommonResult<Teacher> findCourseById(@PathVariable String id){
        QueryWrapper<Course> wrapper = new QueryWrapper<Course>();
        wrapper.eq("status", Course.VIDEO_STATUS_NORMAL);
        wrapper.eq("teacher_id",id);
        List<Course> list = courseService.list(wrapper);
        return CommonResult.ok().data(list);
    }

}
